/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	pmt.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-05-24
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "claw485.h"


/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/

 
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/

claw_t* claw_init(modbus_t* p_modbus,uint8_t address)
{
	claw_t* p_claw = malloc(sizeof(claw_t));
	if(p_claw == NULL)
		return NULL;
	p_claw->p_modbus = p_modbus;
	p_claw->address = address;
	p_claw->status = REG_STATUS_IDLE;
	
	uint16_t data = 0x0001;
	modbus_write(p_claw->p_modbus,p_claw->address,WRITE_SINGLE_REG,REG_CONTROL_MODE,1,&data);
	return p_claw;
}
reg_status_t claw_open(claw_t* p_claw)
{
	if(p_claw == NULL || p_claw->p_modbus == NULL)
		return REG_STATUS_ERR;
	uint16_t data = 0x0001;
	modbus_write(p_claw->p_modbus,p_claw->address,WRITE_SINGLE_REG,REG_OPEN,1,&data);
	p_claw->status = REG_STATUS_OPENING;
	p_claw->poll_tick = 0;
	return p_claw->status;
}
reg_status_t claw_close(claw_t* p_claw)
{
	if(p_claw == NULL || p_claw->p_modbus == NULL)
		return REG_STATUS_ERR;
	uint16_t data = 0x0001;
	modbus_write(p_claw->p_modbus,p_claw->address,WRITE_SINGLE_REG,REG_CLOSE,1,&data);
	p_claw->status = REG_STATUS_CLOSING;
	p_claw->poll_tick = 0;
	return p_claw->status;
}
reg_status_t claw_status(claw_t* p_claw)
{
	if(p_claw == NULL || p_claw->p_modbus == NULL)
		return REG_STATUS_ERR;
	modbus_read(p_claw->p_modbus,p_claw->address,READ_REG,REG_STATUS,1);
	p_claw->poll_tick = 0;
	return p_claw->status;
}
reg_status_t claw_change_address(claw_t* p_claw,uint8_t new_address)
{
//	if(p_claw == NULL || p_claw->p_modbus == NULL)
//		return REG_STATUS_ERR;
//	uint16_t data = new_address;
//	modbus_write(p_claw->p_modbus,p_claw->address,WRITE_SINGLE_REG,REG_ADDR,1,&data);
//	data = 0x0001;
//	modbus_write(p_claw->p_modbus,p_claw->address,WRITE_SINGLE_REG,REG_SAVE,1,&data);
//	
	return p_claw->status;
}